package br.unicamp.mc933;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MapsActivity extends MapActivity {
	private static final String TAG = "MapsActivity";

	private static final int CENTER_LATITUDE = (int) (-22.817723 * 1E6);
	private static final int CENTER_LONGITUDE = (int) (-47.068305 * 1E6);

	private MapView map;
	private MapController controller;

	private LocationManager locationMgr;

	private BusStopMapPoints busStops;

	private MyLocation myLocation;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.maps);

		map = (MapView) findViewById(R.id.map);
		map.setBuiltInZoomControls(true);

		List<Overlay> mapOverlays = map.getOverlays();

		controller = map.getController();

		locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		Location lastLocation = null;

		lastLocation = getLastLocation();

		GeoPoint centerPoint;
		if (lastLocation == null) {
			centerPoint = new GeoPoint(CENTER_LATITUDE, CENTER_LONGITUDE);
			busStops = new BusStopMapPoints(this, getResources().getDrawable(
					R.drawable.icon_by_bus));
		} else {
			centerPoint = new GeoPoint(
					(int) (lastLocation.getLatitude() * 1E6),
					(int) (lastLocation.getLongitude() * 1E6));
			busStops = new BusStopMapPoints(this, getResources().getDrawable(
					R.drawable.icon_by_bus), lastLocation);
		}

		myLocation = new MyLocation(centerPoint, map, getApplicationContext());
		myLocation.enableMyLocation();
		// myLocation.enableCompass();
		mapOverlays.add(myLocation);

		// controller.setCenter(centerPoint);
		// controller.animateTo(centerPoint);
		controller.setZoom(16);

		// Show bus stops in maps
		if (busStops.size() > 0) {
			mapOverlays.add(busStops);
		}

		GPSLocationManager gpsLocationManager = new GPSLocationManager(
				getApplicationContext(), map);

		locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				gpsLocationManager);
	}

	public Location getLastLocation() {
		Location lastLocation = null;

		if (locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			lastLocation = locationMgr
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		} else {
			Toast.makeText(
					getApplicationContext(),
					"GPS esta desativado. Localizacao nao pode ser encontrada.",
					Toast.LENGTH_SHORT).show();
		}

		return lastLocation;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		// when our activity resumes, we want to register for location updates
		myLocation.enableMyLocation();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// when our activity pauses, we want to remove listening for location
		// updates
		myLocation.disableMyLocation();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.maps_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.update_points:
			Location lastLocation = getLastLocation();
			if (lastLocation != null) {
				busStops.updatePoints(lastLocation);
			}
			break;
		}

		return true;
	}

}